/**
 * Created by Administrator on 2018/12/26.
 */
var accordionId;
var roleId;
$(function () {
    roleId = $("#roleId").val();
    roleLimitTool.queryRoleLimit(roleId);
    //console.log("roleLimitTool.getRoleLimit:" + roleLimitTool.getRoleLimit())
    accordionId = "sh-accordion";
    buildMenu();
    /*var content0 = "<div  onclick =\"createTabs('sh-tabs','/notice/manager','公告管理','menu_noticeManager')\"><a  href=\"javascript:void(0)\" id=\"menu_noticeManager"
        + "\" class=\"easyui-linkbutton\" ondragstart=\"return false\" data-options=\"iconCls:'icon-ok'"
        + "\">公告管理</a></div>";
    addPanel("sh-accordion", "系统管理", "icon-add",content0);
    var content1 = "<div  onclick =\"createTabs('sh-tabs','/user/userManager/normal','用户管理','menu3')\"><a  href=\"javascript:void(0)\" id=\"menu3"
        + "\" class=\"easyui-linkbutton\" ondragstart=\"return false\" data-options=\"iconCls:'icon-ok'"
        + "\">用户管理</a></div>";
    content1 += "<div  onclick =\"createTabs('sh-tabs','/limitsManager/normal','权限管理','menu_limits')\"><a  href=\"javascript:void(0)\" id=\"menu_limits"
        + "\" class=\"easyui-linkbutton\" ondragstart=\"return false\" data-options=\"iconCls:'icon-ok'"
        + "\">权限管理</a></div>";
    content1 += "<div  onclick =\"createTabs('sh-tabs','/rolesManager/normal','角色管理','menu_roles')\"><a  href=\"javascript:void(0)\" id=\"menu_roles"
        + "\" class=\"easyui-linkbutton\" ondragstart=\"return false\" data-options=\"iconCls:'icon-ok'"
        + "\">角色管理</a></div>";
    addPanel("sh-accordion", "用户管理", "icon-add", content1);
    var content = "<div  onclick =\"createTabs('sh-tabs','/addMould','添加模具','menu1')\"><a  href=\"javascript:void(0)\" id=\"menu1"
        + "\" class=\"easyui-linkbutton\" ondragstart=\"return false\" data-options=\"iconCls:'icon-ok'"
        + "\">添加模具</a></div>";
    content += "<div  onclick =\"createTabs('sh-tabs','/queryMould','查询模具','menu2')\"><a  href=\"javascript:void(0)\" id=\"menu2"
        + "\" class=\"easyui-linkbutton\" ondragstart=\"return false\" data-options=\"iconCls:'icon-ok'"
        + "\">查询模具</a></div>";
    addPanel("sh-accordion", "模具管理", "icon-add", content);
    selectPanel("sh-accordion","用户管理");*/
    exitfunction();
});

// 构建菜单，选中第一个菜单
var buildMenu = function () {
    //console.log(roleId);
    var menuLimit = queryMenuLimitByRoleId(roleId);
    //console.log(menuLimit);
    var content;
    var selectedMenu = "";
    $.each(menuLimit, function (index, ele) {
        if(null != ele.flag && "title" == ele.flag && ele.limitSort != -1) {
            var pCode = ele.limitCode;
            var pName = ele.limitName;
            var pIcon = ele.icon;
            content = "";
            if(selectedMenu == "") {
                selectedMenu = pName;
            }
            $.each(menuLimit, function (index1, ele1) {
                if(null != ele1.flag && "content" == ele1.flag && ele1.parentCode == pCode) {
                    content +=
                        " <div onclick =\"createMenuTabs('sh-tabs','" + ele1.addr
                        + "','" + ele1.limitName + "','menu_" + ele1.limitCode+"')\">"
                        + "<a  href=\"javascript:void(0)\" id=\"menu_" + ele1.limitCode
                        + "\" class=\"easyui-linkbutton\" ondragstart=\"return false\" "
                        + "data-options=\"iconCls:'" + ele1.icon + "'"
                        + "\">" + ele1.limitName + "</a></div>"
                }
            });
            //console.log(content);
            addPanel(accordionId, pName, pIcon, content);
        }
    });
    selectPanel(accordionId, selectedMenu);
    showOtherPage(menuLimit);
}

var exitfunction = function () {
    $("#exitBtn").click(function () {
        $.messager.confirm("确认对话框","确定退出系统？",
            function (r) {
                if (r) {
                    var jsonstr = postSyncAjax("/user/exitSys", null);
                    //console.log(jsonstr);
                    if(jsonstr.success) {
                        showDialogSetTime("成功退出系统！",function () {
                            window.location.href = "/index";
                        },600);
                    }
                }
            });
    });
}

//显示其他页面
var showOtherPage = function (menuLimit) {
    roleLimitTool.queryRoleLimit();
    var theLimits = roleLimitTool.getLimitObjectByParentCode(-1);
    if(null == theLimits) return;
    var parentCodes = [];
    for(var i = 0; i < theLimits.length; i++) {
        parentCodes.push(theLimits[i].limitCode);
    }
    if(parentCodes.length <= 0) return;

    //console.log(parentCodes);
    //console.log(menuLimit);
    $.each(menuLimit, function (index, ele) {
        if(null != ele.flag && "title" == ele.flag && ele.limitSort == -1) {
            for(var k = 0; k < parentCodes.length; k++) {
                var parentCode = parentCodes[k];
                //console.log(">>>>>>>>>>>parentCode:", parentCode)
                //console.log(roleLimitTool.isLimitBeyondToTheParentCode(parentCode, ele.limitId));
                if(roleLimitTool.isLimitBeyondToTheParentCode(parentCode, ele.limitId)) {
                    var uri = ele.addr;
                    showThePage(parentCode, uri, {
                        url : uri
                    });
                    break;
                }
            }
        }
    });
}